package com.six.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.six.common.BizException;
import com.six.domain.entity.BizOrder;
import com.six.domain.entity.Table;
import com.six.domain.vo.BizOrderDetailVO;
import com.six.domain.vo.BizOrderVO;
import com.six.mapper.BizOrderItemMapper;
import com.six.mapper.BizOrderMapper;
import com.six.mapper.TableMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
@RequiredArgsConstructor
public class BizOrderService {

    private final BizOrderMapper bizOrderMapper;
    private final BizOrderItemMapper bizOrderItemMapper;
    private final TableMapper tableMapper;

    public IPage<BizOrderVO> listByStore(Long storeId, Integer page, Integer size, Integer status) {
        Page<BizOrder> p = new Page<>(page, size);
        LambdaQueryWrapper<BizOrder> qw = Wrappers.lambdaQuery(BizOrder.class)
                .eq(BizOrder::getStoreId, storeId)
                .orderByDesc(BizOrder::getCreateTime);

        if (status != null) {
            qw.eq(BizOrder::getStatus, status);
        }

        return bizOrderMapper.selectPage(p, qw).convert(o -> {
            BizOrderVO vo = new BizOrderVO();
            BeanUtils.copyProperties(o, vo);
            return vo;
        });
    }

    public BizOrderDetailVO detail(Long orderId) {
        BizOrder order = bizOrderMapper.selectById(orderId);
        if (order == null) throw new BizException("订单不存在");

        BizOrderDetailVO vo = new BizOrderDetailVO();
        BeanUtils.copyProperties(order, vo);
        vo.setItems(bizOrderItemMapper.selectByOrderId(orderId));

        // ✅补充桌台信息
        if (order.getTableId() != null) {
            Table table = tableMapper.selectById(order.getTableId());
            if (table != null) {
                vo.setTableNumber(table.getTableNumber());
            }
        }
        return vo;
    }

    @Transactional
    public void updateStatus(Long orderId, Integer status) {
        // 1. 先把原订单查出来，拿 tableId
        BizOrder order = bizOrderMapper.selectById(orderId);
        if (order == null) {
            throw new BizException("订单不存在");
        }
        log.info("商家取消订单，orderId={}, tableId={}, 即将释放桌台", orderId, order.getTableId());

        // 2. 更新订单状态
        LambdaUpdateWrapper<BizOrder> uw = Wrappers.lambdaUpdate(BizOrder.class)
                .set(BizOrder::getStatus, status)
                .eq(BizOrder::getId, orderId);
        bizOrderMapper.update(null, uw);

        // 3. ✅ 仅当“改成已取消”且订单原来绑定了桌台时，释放桌台
        if (status == 3 && order.getTableId() != null) {
            tableMapper.updateStatus(order.getTableId(), "FREE");
        }
    }
}